<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'home';
$return['ischecklogin']=cfc('core')->checklogin('','',true);
$return['pluginset']['ailogo']=tomedia($return['pluginset']['ailogo']);
$return['basic']=$return['pluginset'];
$worker = pdo_get('jy_msgorder_worker',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'status'=>1));
if(empty($worker)){
	exi('您不是专家！','error','/index/');
}
if($_RQ['op']=='home'){
	$id=intval($_RQ['id']);
	$item=pdo_get('jy_msgorder_tcp',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	$worker=pdo_get('jy_msgorder_worker',array('id'=>$item['workerid'],'uniacid'=>$_SESSION['uniacid']));
	if(!$item['id']){
		exi('请从询单进入对话！','error','/index/');
	}
	if($item['workerid']!=$worker['id']){
		exi('该询单不是您的！','error','/index/');
	}
	$worker['logo']=tomedia($worker['logo']);
	$worker['onlinetime']=json_decode($worker['onlinetime'],true);
	$worker['tags']=json_decode($worker['tags'],true);
	$worker['api_data']=json_decode($worker['api_data'],true);
	$worker['salenum']=pdo_count('jy_msgorder_tcp',array('uniacid'=>$_SESSION['uniacid'],'workerid'=>$item['workerid']));
	$item['worker']=$worker;
	$item['createtime']=date('Y-m-d H:i:s', $item['createtime']);
	$item['statustitle']=$this->tcp_status[$item['status']];
	$member=cfc('core')->member_show($_SESSION['uid']);
	$return['openid']= $_SESSION['openid'];
	$return['member']= $member;
	$return['item']= $item;
	$return['list']= array();
	$return['post']= array('msg'=>'','tcpid'=>$item['id'],'worker'=>$item['workerid']);
	$return['params']= array('tcpid'=>$item['id']);
	set_html('title','询单记录');
	exi($return);
}
if($_RQ['op']=='list'){
	$tcpid=$_RQ['tcpid'];
	$params=array('uniacid'=>$_SESSION['uniacid'],'tcpid'=>$tcpid);
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total = 0;
	$list=pdo_getslice('jy_msgorder_tcplog',$params,array($pindex,$psize),$total,$fields,'','id desc');
	$chatlogtime = time();
	if($list){
		foreach($list as $k=>&$v){
					$settime = $v['createtime'];
					if(($chatlogtime - $v['createtime']) > 600 || $k+1==count($list)){
						$v['createtime'] = date('Y-m-d H:i:s',$v['createtime']);
					}else{
						$v['createtime'] = '';
					}
					$chatlogtime = $settime;
					$v['msg'] = cfc('tools')->filterword($v['msg'],$setting['filterword']);
					$v['msg'] = preg_replace('/\xEE[\x80-\xBF][\x80-\xBF]|\xEF[\x81-\x83][\x80-\xBF]/', '[无法识别字符]', $v['msg']);
					$hrefhtml = '@(?i)\b((?:[a-z][\w-]+:(?:/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?«»“”‘’]))@';
					preg_match_all($hrefhtml,$v['msg'],$resarray);  
					if($v['type'] == 1 || $v['type'] == 2){
						foreach($resarray[0] as &$vv){
							if(!empty($vv)){
								$v['msg'] = str_replace($vv,"<a href='".$vv."'>".$vv."</a>",$v['msg']);
							}
						}
					}
					if(!$_SESSION['wxapp_id'] || $_SESSION['fromh5wxapp']){
						$v['answer'] = str_replace("\n","</br>",$v['answer']);
					}
		}
	}
	$return['list']= $list;
	exi($return);
}
if($_RQ['op']=='gethtml'){
	$id=intval($_RQ['id']);
	$chatid=intval($_RQ['chatid']);
	$getmemberlogo=$this->session('getmemberlogo');
	$memberlogo=$this->session('memberlogo');
	$info=pdo_get('jy_msgorder_tcplog',array('uniacid'=>$_SESSION['uniacid'],'chatid'=>$chatid,'id'=>$id));
	if($_SESSION['os']=='windows'){
		$htmltime = '<div class="message_box_time">'.$info['createtime'].'</div>';
		if($info['type'] == 1 || true){
			$html .= $htmltime;
			if($info['setmid'] != $_SESSION['uid']){
					$html .='<div class="message_box"><img class="mssage_box_pic" src="'.$getmemberlogo.'" style="display:inline-block;"/><div class="message_content"><div class="message_content_inner"><p>'.$info['answer'].'</p></div></div></div>';
			}else{
					$html .='<div class="message_box from_self"><img class="mssage_box_pic" src="'.$memberlogo.'" style="display:inline-block;"/><div class="message_content"><div class="message_content_inner"><p>'.$info['askmsg'].'</p></div></div></div>';
			}			
		}
	}else{
		if($info['type'] == 1 || true){
			if($info['setmid'] == $_SESSION['uid']){
				$html .='<div class="r clearfix"><img class="pic"/><p class="text">'.$info['askmsg'].'</p></div>';
			}else{
				$html .='<div class="l clearfix"><img class="pic"/><p class="text">'.$info['answer'].'</p></div>';
			}
			$html .= $htmltime;
		}
	}
	$return['html']= $html;
	exi($return);
}
if($_RQ['op']=='addlog'){
	$postdata=get_postdata();
	$postdata['is_worker']=1;
	$logid=$this->model('kefu')->addchatlog($postdata);
	$res['log']=array(
		'id'=>$logid,
		'mid'=>$_SESSION['uid'],
		'tcpid'=>$postdata['tcpid'],
		'workerid'=>$postdata['workerid'],
		'msg'=>$postdata['msg'],
		'is_worker'=>1,
	);
	exi($res);
}